package com.woniuxy.microsysserver.controller;

import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.common.util.JWTUtil;
import com.woniuxy.microsysserver.bean.AdminInfo;
import com.woniuxy.microsysserver.service.IAdminService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("sys")
public class AdminController {

    /**
     * login功能队列
     */
    private static final String QUEUE_NAME = "login_queue";

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private HashOperations hashOperations;
    @Resource
    private IAdminService adminServiceImpl;

    @PutMapping("/add")
    ResultMsg addAdmin(AdminInfo adminInfo){
        System.out.println(adminInfo);
        adminServiceImpl.saveAdminInfo(adminInfo);
        //修改角色的个数
        return new ResultMsg(true);
    }

    @GetMapping(value = "/all" ,produces = MediaType.APPLICATION_JSON_VALUE )
    ResultMsg findAll(){
        ResultMsg resultMsg = new ResultMsg(true);
        List<AdminInfo> all = adminServiceImpl.findAll();
//        all.forEach(e -> System.out.println(e));
        resultMsg.setData(all);
        return resultMsg;
    }

    @PostMapping(value = "/findByCondition",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg findByCondition(@RequestBody   AdminInfo adminInfo){
        ResultMsg resultMsg = new ResultMsg(true);
        List<AdminInfo> all = adminServiceImpl.findByCondition(adminInfo);
        resultMsg.setData(all);
        return resultMsg;
    }

    @PutMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg delete(@PathVariable("id") Integer id){
        System.out.println(id);
        adminServiceImpl.delete(id);
        return new ResultMsg(true);
    }

    @PutMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg updateAdmin(@RequestBody AdminInfo adminInfo){
        System.out.println(adminInfo);
        adminServiceImpl.update(adminInfo);
        AdminInfo newAdminInfo = adminServiceImpl.findById(adminInfo.getId());
        //放入redis
        hashOperations.put("admins", ""+newAdminInfo.getId(), newAdminInfo);
        return new ResultMsg(true);
    }

    @GetMapping(value = "/groupByRole" ,produces = MediaType.APPLICATION_JSON_VALUE )
    ResultMsg findByRole( Integer rid){
        List<AdminInfo> all = adminServiceImpl.findByRole(rid);
        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(all);
        return resultMsg;
    }

    @GetMapping(value = "/allFromRedis" ,produces = MediaType.APPLICATION_JSON_VALUE )
    ResultMsg findAllFromRedis(){

        List admins = hashOperations.values("admins");




        ResultMsg resultMsg = new ResultMsg(true);
        resultMsg.setData(admins);
        return resultMsg;

    }

    @PostMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg login(String loginName,String password){
//        System.out.println(loginName);
//        System.out.println(password);
        AdminInfo adminInfo = adminServiceImpl.login(loginName);

        ResultMsg resultMsg = new ResultMsg(true);
        if(adminInfo == null){
            resultMsg.setCode(9092);
            resultMsg.setMessage("账号不存在");
            return resultMsg;
        }else {
            if(adminInfo.getState() == 3){
                resultMsg.setCode(9093);
                resultMsg.setMessage("您已被炒鱿鱼");
                return resultMsg;
            }
            if(adminInfo.getPassword().equals(password) ){
//                System.out.println(adminInfo.getPassword());
                resultMsg.setData(adminInfo);
                //token
                //redis放数据

                //如果认证通过了，就直接产生JWT令牌
                Map<String,Object> claims = new HashMap<>();
                claims.put("loginName",loginName);
                String token = JWTUtil.createToken(claims);

                Map<String,Object> result = new HashMap<>();
                result.put("token",token);
                adminInfo.setPassword("");
                result.put("admin", adminInfo);
                resultMsg.setData(result);
                return resultMsg;
            }else {
//                System.out.println(adminInfo.getPassword());
                resultMsg.setCode(9091);
                resultMsg.setMessage("密码错误");
            }
        }
        return resultMsg;
    }

    @PostMapping(value = "/changeHead",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg changeHead(@RequestBody AdminInfo adminInfo){

        adminServiceImpl.changeHead(adminInfo);
        return new ResultMsg(true);
    }

    /**
     * 导入消息推送对象
     */
    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;

    @PostMapping(value = "/send",produces = MediaType.APPLICATION_JSON_VALUE)
    public void send(Integer id){
        //我们使用这个方法进行消息的转发发送！
        AdminInfo admin = adminServiceImpl.findById(id);
        simpMessagingTemplate.convertAndSend("/topic/getLogin", admin.getAdminName()+"登录了");
    }
}
